//
//  XMConfirmServiceView.m
//  XMJJ_zmy
//
//  Created by 丁浩 on 2025/7/9.
//

#import "XMConfirmServiceView.h"

@interface XMConfirmServiceView ()

@property (nonatomic, strong) UIView *contentBgView;

@end

@implementation XMConfirmServiceView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.backgroundColor = kColorHEXA(0x464646, 0.46);
    [self addSubview:self.contentBgView];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(_contentBgView.width - KScaleWidth(6.0) - KScaleWidth(30.0), KScaleWidth(10.0), KScaleWidth(30.0), KScaleWidth(30.0)) image:@"" selectImg:@""];
    [cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [_contentBgView addSubview:cancelBtn];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), KScaleWidth(60.0), _contentBgView.width - KScaleWidth(16.0), KScaleWidth(25.0)) font:KBoldFontSize(24.0) textColor:kColorHEX(0x333333)];
    titleLabel.text = @"请阅读并同意以下协议";
    [_contentBgView addSubview:titleLabel];
    
    UIButton *serviceBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(KScaleWidth(10.0), titleLabel.maxY + KScaleWidth(6.0), KScaleWidth(100.0), KScaleWidth(41.0)) title:@"《用户协议》" titleColor:kColorHEX(0x3D5A99)];
    [serviceBtn addTarget:self action:@selector(chatServiceAction:) forControlEvents:UIControlEventTouchUpInside];
    [_contentBgView addSubview:serviceBtn];
    UILabel *andLabel = [[UILabel alloc] initWithFrame:CGRectMake(serviceBtn.maxX, serviceBtn.y, KScaleWidth(15.0), serviceBtn.height) font:KFontSize(15.0) textColor:kColorHEX(0x988DA2) textAlignment:NSTextAlignmentCenter];
    andLabel.text = @"和";
    [_contentBgView addSubview:andLabel];
    
    UIButton *secretBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(andLabel.maxX, andLabel.y, serviceBtn.width, serviceBtn.height) title:@"《隐私协议》" titleColor:kColorHEX(0x3D5A99)];
    [secretBtn addTarget:self action:@selector(chatSecretAction:) forControlEvents:UIControlEventTouchUpInside];
    [_contentBgView addSubview:secretBtn];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(17.0) frame:CGRectMake((_contentBgView.width - KScaleWidth(320.0))/2, secretBtn.maxY + KScaleWidth(58.0), KScaleWidth(320.0), KScaleWidth(44.0)) title:@"同意并继续" titleColor:kColorHEX(0x3333333)];
    [confirmBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
    [confirmBtn setCornerRadius:confirmBtn.height/2];
    [confirmBtn addTarget:self action:@selector(confirmAction:) forControlEvents:UIControlEventTouchUpInside];
    [_contentBgView addSubview:confirmBtn];
    
    self.alpha = 0;
    _contentBgView.y = self.height;
}

#pragma mark - BtnAction
- (void)cancelAction:(UIButton *)button {
    [self disMiss];
}

- (void)confirmAction:(UIButton *)button {
    self.confirmBlock ? self.confirmBlock() : nil;
    [self disMiss];
}

- (void)chatServiceAction:(UIButton *)button {
    self.checkServiceBlock ? self.checkServiceBlock(@"4") : nil;
}

- (void)chatSecretAction:(UIButton *)button {
    self.checkServiceBlock ? self.checkServiceBlock(@"5") : nil;
}

- (void)show {
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.alpha = 1;
        weakSelf.contentBgView.y = weakSelf.height - weakSelf.contentBgView.height;
    }];
}

- (void)disMiss {
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.alpha = 0;
        weakSelf.contentBgView.y = weakSelf.height;
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

- (UIView *)contentBgView {
    if (!_contentBgView) {
        _contentBgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - KScaleWidth(310.0), self.width, KScaleWidth(310.0))];
        _contentBgView.backgroundColor = kColorHEX(0xffffff);
        [_contentBgView setPartCornerWithRectCorner:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadius:KScaleWidth(12.0)];
    }
    return _contentBgView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
